var basePath=basePath();
var access_token = GetUrl("access_token");
$(function () {

    console.log("get detail in ");
    getProductInfo(GetUrl("pid"));

    showBuyBtn();
    
    $("#buyLimit").click(function(){
        if(status==1){
                    var id = GetUrl("pid");
                    if(isNaN(id)){
                        layer.open({content: "信息缺失，请刷刷再试！", skin: 'msg', time: 2});
                    }
                    //判断是否会会员
                    $.ajax({
                        url:basePath+"/v1/user/access_token",
                        type:'GET',
                        data:{
                            access_token:access_token
                        },
                        dataType:"json",
                        success:function(json) {
                            if (json.code == 1000) {
                                var userInfo = json.data;
                                var userMark =userInfo.user_mark;
                                if(userMark==4){
                                    window.location.href= basePath+"res/html/gwcsubup.html?id="+id+"&access_token="+access_token;
                                }else{
                                    layer.open({
                                        title: ['该商品为会员专享', 'background-color: #FFF;'],
                                        content: '<div style="text-align: left; ">购买羊仓商城中<span style="color: red;">富硒蛋卡</span>（月卡、季卡、年卡），并通过羊仓微信服务号“羊仓会员专属”成<span style="color: red;">激活蛋卡</span>，即可成为羊仓会员</div>'
                                        ,btn: ['<span style="color:#000;">激活蛋卡</span>','<span style="color:#000;">购买蛋卡</span>' ]
                                        ,yes: function(index){//激活
                                            layer.close(index);
                                            window.location.href= "http://b.yangcangclub.com/eggcard/h5/user/order/list";
                                            // window.location.href=basePath+"res/mryx.html?access_token="+access_token+"&type=1";
                                        },
                                        no: function (index) {//购买
                                            layer.close(index);
                                            window.location.href=basePath+"res/mryx.html?access_token="+access_token+"&type=1";
                                        }
                                    });
                                }
                            }
                        },
                        error:function(data){
                            console.info(data.msg);
                        }
                    });
        }else{
            layer.open({content: "时间未到", skin: 'msg', time: 2});
        }
    });
    
});

function showBuyBtn(){
    if(status==1){
        $("#buyLimit").show();
    }else{
        $("#buyLimit").hide();
    }
}

var status=2;//1为可以卖，2为时间未到
//ajax获取商品详情
function getProductInfo(id){
    $.ajax({
        url:"/yangcangclub/v1/merchandise/detail?access_token="+GetUrl("access_token")+"&pid="+id,
        type:'GET',
        dataType:"json",
        success:function(data){
            if(data.code == 1000){
                if(data.data==undefined){return ;}
                data=data.data;
                $("#bannerUrl").attr("src",data.bannerUrl);
                $("#productName").html(data.name);
                $("#productPrice").html("￥"+data.unitPrice+".00");
                if(data.showUnitPrice>0){
                    $("#showPrice").html("价格："+data.showUnitPrice+".00");
                }
                $(".c-cont").html(data.content);

                // 计算时间
                var now=new Date(data.now.replace(/-/g,"/"));
                var start=new Date(data.startTime.replace(/-/g,"/"));
                var end=new Date(data.endTime.replace(/-/g,"/"));
                var stat=data.sellStatue;//1为可以卖，2为时间未到
                if(stat==2){
                    t=start.getTime()-now.getTime();
                    $("#showTitle").html("离开始还剩");
                }else{
                    t=end.getTime()-now.getTime();
                    status=1;
                }
                showBuyBtn();
            }else{
                layer.open({content: data.msg, skin: 'msg', time: 2});
            }
        },
        error:function(data){
            layer.open({content: '请求失败，请刷新再试', skin: 'msg', time: 2});
        }
    });
    
}


